<?php defined("MAIN_DIR") or die("No direct access");

$add_error = false;
if (isset($_POST['action']) && $_POST['action'] == "add")
{
	$days = $sizes = $cost = "";
	for ($i=0; $i <= $_POST['sizes']; $i++)
	{
		for ($j=0; $j <= $_POST['days']; $j++)
		{
			if ($j == 0 && $i == 0)
				;
			elseif ($i == 0)
			{
				$days .= $_POST['day_'.$j];
				if ($j < $_POST['days'])
					$days .= ",";
			}
			elseif ($j == 0)
			{
				$sizes .= $_POST['size_'.$i];
				if ($i < $_POST['sizes'])
					$sizes .= ",";
			}
			else
			{
				$cost .= $_POST['cost_'.$j.'_'.$i];
				if ($j < $_POST['days'] || $i < $_POST['sizes'])
					$cost .= ",";
			}
		}
	}
	
	$address = trim(filter_var($_POST['address'],FILTER_SANITIZE_MAGIC_QUOTES));
	$phone = trim(filter_var($_POST['phone'],FILTER_SANITIZE_MAGIC_QUOTES));
	$days = trim(filter_var($days,FILTER_SANITIZE_MAGIC_QUOTES));
	$sizes = trim(filter_var($sizes,FILTER_SANITIZE_MAGIC_QUOTES));
	$cost = trim(filter_var($cost,FILTER_SANITIZE_MAGIC_QUOTES));
	$add_query = "insert into `box` (`bank_id`,`address`,`phone`,`days`,`dimensions`,`cost`) values ('$_POST[bank]','$address','$phone','$days','$sizes','$cost')";
	if (mysql_query($add_query)) {
		$box = mysql_fetch_array(mysql_query("select * from `box` order by `box_id` desc limit 1"));
		to("/admin/boxes/edit/$box[box_id]");
	} else {
		$add_error = true;
	}
}

require_once ADMIN.HEADFILE;
?>
<style>
.right_line { border-right: 1px #888888 dotted }
.bottom_line { border-bottom: 1px #888888 dotted }
#box input { width: 110px; }
.buttons span
{
	cursor: pointer;
	text-decoration: underline;
	line-height: 25px;
}
</style>
<script type="text/javascript" src="/files/js/admin/boxes.js"></script>
<div align="left">
<h1>Добавить ячейку</h1>
<hr /><?php
if ($add_error)
	echo "<p style='color:red'>Не удалось добавить</p>";
?><form method='POST'>
<input type='hidden' name='action' value='add' />
<input type='hidden' name='days' id='days' value='1' />
<input type='hidden' name='sizes' id='sizes' value='1' />
<table cellpadding='5' cellspacing='0'>
	<tr>
		<td>Банк</td>
		<td>
			<select style="width:200px" name='bank'><?php
			$bank_res = mysql_query("select `bank_id`,`name` from `bank` where `city_id` = '$_SESSION[city_admin]' order by `name`");
			while ($bank_row = mysql_fetch_array($bank_res))
			{
				echo "<option value='$bank_row[bank_id]'";
				if (isset($_POST['bank']) && $bank_row['bank_id'] == $_POST['bank'])
					echo " selected='selected'";
				elseif (isset($_GET['bank_id']) && $bank_row['bank_id'] == $_GET['bank_id'])
					echo " selected='selected'";
				echo ">$bank_row[name]</option>";
			}
			?></select>
		</td>
	</tr>
	<tr>
		<td width="150px">Адрес</td>
		<td width="500px">
			<input style='width:100%' type='text' name='address' value='<?php if (isset($_POST['address'])) echo $_POST['address'];?>' />
		</td>
	</tr>
	<tr>
		<td width="150px">Телефон</td>
		<td width="500px">
			<input style='width:100%' type='text' name='phone' value='<?php if (isset($_POST['phone'])) echo $_POST['phone'];?>' />
		</td>
	</tr>
	<tr>
		<td colspan='2' style="border:0px gray solid">
			<table cellpadding="5" cellspacing="0" id="box">
				<tr id="0">
					<td class="bottom_line right_line">Размеры \ Период</td>
					<td class="bottom_line"><input type='text' name='day_1' value="Новый период" /></td>
				</tr>
				<tr id="1">
					<td class="right_line"><input type='text' name='size_1' value="Новый размер" /></td>
					<td><input type='text' name='cost_1_1' value="Новая стоймость" /></td>
				</tr>
			</table>
			<div class="buttons">
				<span id="add_day">Добавить период</span> - <span id="del_day">Убрать период</span><br />
				<span id="add_size">Добавить размер</span> - <span id="del_size">Убрать размер</span>
			</div>
		</td>
	</tr>
	<tr valign='top'>
		<td colspan='2' align="right"><button style="width:100px;height:30px" type="submit">Добавить</button></td>
	</tr>
</table>
</form>
<br /><hr /><br />
</div>

<?php require_once ADMIN.FOOTERFILE; ?>